package com.zhuiyun.pm.sys.user.service.Impl;

import com.zhuiyun.pm.page.MyPageHelper;
import com.zhuiyun.pm.page.PageRequest;
import com.zhuiyun.pm.page.PageResult;
import com.zhuiyun.pm.result.CommonResult;
import com.zhuiyun.pm.sys.user.mapper.SysUserMapper;
import com.zhuiyun.pm.sys.user.entity.SysUserEntity;
import com.zhuiyun.pm.sys.user.service.SysUserService;
import com.zhuiyun.pm.utils.UploadFileUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*
 * @Author 赵康成
 * @Description 对用户表做出操作
 * @Date  2023-03-13 10:12:19
 * @Return
 */

@Service
public class SysUserServiceImpl implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 用户通过登录名密码进行登录
     * @param loginName,password
     * @return SysUserEntity
     */

    //SUPPORTS（supports）：如果当前有事务，则使用事务;如果当前没有事务，则不使用事务。
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public SysUserEntity queryUserForLogin(String loginName, String password) {
        Example userExample = new Example(SysUserEntity.class);
        Example.Criteria userCriteria = userExample.createCriteria();

        //去数据库中比对loginName和userPassword 参照实体类中定义的变量名称
        userCriteria.andEqualTo("loginName",loginName);
        userCriteria.andEqualTo("password",password);

        SysUserEntity result = sysUserMapper.selectOneByExample(userExample);
        return result;

    }

    /**
    * @Description 分页获取后台用户管理信息列表
    * @Author lzy
    * @Date 2023/3/14 19:02
    * @Return com.zhuiyun.pm.result.CommonResult
    * @param pageRequest
    * @param loginName
    * @param nickName
    */
    @Override
    public CommonResult getSysUserListAll(PageRequest pageRequest, String loginName, String nickName) {
        MyPageHelper.startPage(pageRequest);
        Example example = new Example(SysUserEntity.class);
        Example.Criteria criteria = example.createCriteria();
        Integer delFlag = 0;
        criteria.andLike("nickName", "%" + nickName + "%").andEqualTo("delFlag", delFlag);
        if (loginName != null && !"".equals(loginName)){
            criteria.andEqualTo("loginName",loginName);
        }
        List<SysUserEntity> userEntities = sysUserMapper.selectByExample(example);
        PageResult pageResult = MyPageHelper.pageInfo(userEntities);
        return CommonResult.ok(pageResult);
    }

    /**
    * @Description 根据id删除用户信息
    * @Author lzy
    * @Date 2023/3/14 19:12
    * @Return com.zhuiyun.pm.result.CommonResult
    * @param id
    */
    @Override
    public CommonResult deleteSysUserById(Integer id) {
        SysUserEntity sysUserEntity = new SysUserEntity();
        sysUserEntity.setId(id);
        int i = sysUserMapper.deleteByPrimaryKey(sysUserEntity);
        if (i > 0) {
            return CommonResult.ok(i);
        }
        return CommonResult.error("401");
    }

    /**
    * @Description 新增给用户信息
    * @Author lzy
    * @Date 2023/3/14 20:52
    * @Return com.zhuiyun.pm.result.CommonResult
    * @param multipartRequest
    * @param sysUserEntity
    */
    @Override
    public CommonResult saveSysUser(MultipartRequest multipartRequest, SysUserEntity sysUserEntity) {
        MultipartFile file = multipartRequest.getFile("file");
        String s = UploadFileUtils.uploadFile(file);
        sysUserEntity.setCreateTime(new Date());
        sysUserEntity.setUpdateTime(new Date());
        sysUserEntity.setDelFlag(0);
        sysUserEntity.setAvatarId(Integer.valueOf(s));
        String md5Password = DigestUtils.md5DigestAsHex(sysUserEntity.getPassword().getBytes());
        sysUserEntity.setPassword(md5Password);
        int i = sysUserMapper.insertSelective(sysUserEntity);
        if (i > 0) {
            return CommonResult.ok(i);
        }
        return CommonResult.error("501");
    }

    /**
    * @Description 校验登录名称是否已存在
    * @Author lzy
    * @Date 2023/3/14 20:28
    * @Return com.zhuiyun.pm.result.CommonResult
    * @param loginName
    */
    @Override
    public CommonResult checkLoginName(String loginName) {
        SysUserEntity sysUserEntity = new SysUserEntity();
        sysUserEntity.setLoginName(loginName);
        int i = sysUserMapper.selectCount(sysUserEntity);
        if (i > 0) {
            return CommonResult.ok(i);
        }
        return CommonResult.error("501");
    }
}
